#!/bin/bash
#
# This script requires lcov, dirname
#

TESTS="\
unit_bridge \
unit_buffer \
unit_cleanup \
unit_client \
unit_config \
unit_driver \
unit_eventloop \
unit_fmq \
unit_ipc \
unit_local_object \
unit_local_reply \
unit_local_request \
unit_log \
unit_protocol \
unit_proxy_object \
unit_reader \
unit_remote_object \
unit_remote_reply \
unit_remote_request \
unit_servicemanager \
unit_servicemanager_aidl \
unit_servicemanager_aidl2 \
unit_servicemanager_aidl3 \
unit_servicemanager_aidl4 \
unit_servicemanager_hidl \
unit_servicename \
unit_servicepoll \
unit_writer"

function err() {
    echo "*** ERROR!" $1
    exit 1
}

# Check the required tools
which lcov >> /dev/null || err "Please install lcov"
which dirname >> /dev/null || err "Please install dirname"

# LCOV 1.10 has branch coverage disabled per default
# Previous versions didn't have the --rc option
if  [ ! -z "$(lcov --help | grep ' --rc ')" ] ; then
    LCOV_OPT="--rc lcov_branch_coverage=1"
    GENHTML_OPT="--branch-coverage"
fi

pushd `dirname $0` > /dev/null
COV_DIR="$PWD"
pushd .. > /dev/null
TEST_DIR="$PWD"
pushd .. > /dev/null
TOP_DIR="$PWD"
popd > /dev/null
popd > /dev/null
popd > /dev/null

make -C "$TOP_DIR" clean
for t in $TESTS ; do
    pushd "$TEST_DIR/$t"
    make -C "$TEST_DIR/$t" clean coverage || exit 1
    build/coverage/$t || exit 1
    popd
done

# Sometimes you need this, sometimes that :S
BASE_DIR="$TOP_DIR"
#BASE_DIR="$TOP_DIR/src"

FULL_COV="$COV_DIR/full.gcov"
LIB_COV="$COV_DIR/lib.gcov"
rm -f "$FULL_COV" "$LIB_COV"
lcov $LCOV_OPT -c -d "$TOP_DIR/build/coverage" -b "$BASE_DIR" -o "$FULL_COV" || exit 1
lcov $LCOV_OPT -e "$FULL_COV" "$BASE_DIR/*" -o "$LIB_COV" || exit 1
genhtml $GENHTML_OPT "$LIB_COV" -t "libgbinder" --output-directory "$COV_DIR/report" || exit 1
